<?php 
class book_list extends List_Temp_Tag{
	/*
		給 基类一个标示
	*/
	function __construct($arg){
		$defults = array(
			'page' 		=> $this->get_page(),
			'par_page'  => $this->get_par_page(3)
		);
		$args 			= array_merge($defults,$arg);	
		//避免分页数据不同
		$this->set_par_page($args['par_page']);
		
		//如果设置了limit就不需要通过page来设置limit!
		if(empty($args['limit'])){										
			$args['offset'] = ($args['page']-1)*$args['par_page'];
			$args['limit']  =  $args['par_page'];
		}
		$this->arg=$args;
		parent::__construct('图书');	
	}
	/*
		每一个List_Temp_Tag子类都在这里获取数据
	*/
	function get(){
		//$this->par_paged = 12;
		$results = self::select($this->arg);
		//设置总共有多少记录集
		$this->set_total_page($results['total']);
		
		return $results['page'];
	}
	function get_current_catname($cat_id){
		global $wpdb;
		$cat_name=$wpdb->get_var('select cat_name from corp_categories where cat_id='.$cat_id);	
		return $cat_name;
	}
	/*
		查询数据
	*/	
	static function  select($arg=array()){
		global $wpdb;			
		
		$defults = array(
				'cat_id'		=> false,
				'id'	        => false,
				'limit'			=> false,
				'offset'		=> 0,
				'order'			=> 'date',
				'desc'			=> 'desc'
			);
		$args = array_merge($defults,$arg);	
		
		extract($args);
		
		$select_sql['select']	= 'select * from corp_image_media ';
		
		if($cat_id)
				$select_sql['where'][]  = " cat_id = {$cat_id} ";
		if($id)
				$select_sql['where'][]  = " id = {$id} ";
		if($is_recommend)
				$select_sql['where'][]  = " is_recommend = {$is_recommend} ";	
		$t_where = false ;
		if(!empty($select_sql['where']))
				$t_where = " where " . join( ' and ', (array)$select_sql['where'] );
		$t_sql = $select_sql['select'] .$t_where ;
		
		if($order)
				$t_sql = $t_sql." order by {$order} {$desc} ";				
		if($limit)
				$t_sql = $t_sql.'limit '.$offset.','.$limit;
		
		if(!$page = $wpdb->get_results($t_sql))
					return false;	
		
		//total
		$total_sql = 'select count(*) from corp_image_media ' .$t_where;
			
		if(!$total = $wpdb->get_var($total_sql))
				return false;		
		return array('page'=>$page,'total'=>$total);	
	}
}

/*得到推荐的产品*/
function get_recommend_product($limit=2){
	return new book_list(array(
								"is_recommend"=>1,
								'limit'=>$limit	
							));
}
function get_recomment_product_parentcat($cat_id){
	global $wpdb;
	$parent_id=$wpdb->get_var('select cat_parent from corp_categories where cat_id='.$cat_id);
	$product=new book_list(array());
	$product_cat=$product->get_current_catname($parent_id);
	if($product_cat=="图书类"){
		return "book";
	}else{
		return "media";	
	}
}
?>